package fitdroid.helpers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;

public class timehelper {

	public String now(String dateFormat) {
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
	    return sdf.format(cal.getTime());

	  }

	public int converttimeseconds (String time)
	{
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		
		int counter =tokenizer.countTokens();

		
		int res=0;
		while (tokenizer.hasMoreTokens())
		{
			if (counter==3)
			{
				res+= Integer.valueOf(tokenizer.nextToken())*60*60;
			}
			else
				if (counter==2)
				{
					res+= Integer.valueOf(tokenizer.nextToken())*60;
				}
				else
				{
					res+= Integer.valueOf(tokenizer.nextToken());
				}
			counter--;
		}
		return res;
	}
	
	public double converttimeminutes (String time)
	{
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		int counter =tokenizer.countTokens();
		
		double res=0;
		while (tokenizer.hasMoreTokens())
		{
			if (counter==3)
			{
				res+= Integer.valueOf(tokenizer.nextToken())*60;
			}
			else
				if (counter==2)
				{
					res+= Integer.valueOf(tokenizer.nextToken());
				}
				else
				{
					res+= (Double.valueOf(tokenizer.nextToken())/60);
				}
			counter++;
		}
		return res;
	}
	
	public String secondstotime (int s)
	{
		   int hours = s/3600;
		  int minutes = ((s-(hours*3600))/60);
		  int seconds = (s-(hours*3600)-(minutes*60));
		
		  String timeres="";
		  if (hours<10)
			  	timeres+="0"+hours+":";
		  else
			  	timeres+=hours+":";
	      if (minutes<10)
	    	  timeres+="0"+minutes+":";
		  else
			  	timeres+=minutes+":";
	      if (seconds<10)
	    	  timeres+="0"+seconds;
		  else
			  	timeres+=seconds;
	      
		  return timeres;
	}
	
	public String getonehourplus(String time){
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		if (tokenizer.countTokens() != 3) {return "no valid format";} else
		{
		String hours = tokenizer.nextToken();
		int hours2 = Integer.valueOf(hours);
		if (hours2 < 59)
		hours2++;
		hours = String.valueOf(hours2);
		
		if (hours.length()< 2) hours = "0" + hours;
		
		return hours+":"+tokenizer.nextToken()+":"+tokenizer.nextToken();

		}
	}
	
	public String getonehourminus(String time){
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		if (tokenizer.countTokens() != 3) {return "no valid format";} else
		{
		String hours = tokenizer.nextToken();
		int hours2 = Integer.valueOf(hours);
		if (hours2 != 0) {
		hours2--;
		}
		hours = String.valueOf(hours2);
		
		if (hours.length()< 2) hours = "0" + hours;
		
		return hours+":"+tokenizer.nextToken()+":"+tokenizer.nextToken();

		}
	}
	
	public String getoneminuteplus(String time){
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		if (tokenizer.countTokens() != 3) {return "no valid format";} else
		{
		String hours = tokenizer.nextToken();
		String minutes = tokenizer.nextToken();
		int minutes2 = Integer.valueOf(minutes);
		if (minutes2 < 59)
		minutes2++;
		minutes = String.valueOf(minutes2);
		
		if (minutes.length()< 2) minutes = "0" + minutes;
		
		return hours+":"+ minutes +":"+tokenizer.nextToken();
		}
	}
	
	public String getoneminuteminus(String time){
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		if (tokenizer.countTokens() != 3) {return "no valid format";} else
		{
		String hours = tokenizer.nextToken();
		String minutes = tokenizer.nextToken();
		int minutes2 = Integer.valueOf(minutes);
		if (minutes2 != 0)
		minutes2--;
		minutes = String.valueOf(minutes2);
		
		if (minutes.length()< 2) minutes = "0" + minutes;
		
		return hours+":"+minutes+":"+tokenizer.nextToken();
		}
	}
	
	public String getonesecondplus(String time){
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		if (tokenizer.countTokens() != 3) {return "no valid format";} else
		{
		String hours = tokenizer.nextToken();
		String minutes = tokenizer.nextToken();
		String seconds = tokenizer.nextToken();
		int seconds2 = Integer.valueOf(seconds);
		if (seconds2 < 59)
		seconds2++;
		seconds = String.valueOf(seconds2);
		
		if (seconds.length()< 2) seconds = "0" + seconds;
		
		return hours+":"+minutes+":"+seconds;
		}
	}
	public String getonesecondminus(String time){
		StringTokenizer tokenizer = new StringTokenizer(time,":");
		if (tokenizer.countTokens() != 3) {return "no valid format";} else
		{
		String hours = tokenizer.nextToken();
		String minutes = tokenizer.nextToken();
		String seconds = tokenizer.nextToken();
		int seconds2 = Integer.valueOf(seconds);
		if (seconds2 != 0)
		seconds2--;
		seconds = String.valueOf(seconds2);
		
		if (seconds.length()< 2) seconds = "0" + seconds;
		
		return hours+":"+minutes+":"+seconds;
		}
	}
	
	public String calculatedifference(String time1, String time2){
		
		StringTokenizer tokenizer1 = new StringTokenizer(time1,":");
		StringTokenizer tokenizer2 = new StringTokenizer(time1,":");
		
		if (tokenizer1.countTokens() != 3) {return "no valid format";} else
		if (tokenizer2.countTokens() != 3) {return "no valid format";} else
			
		{
	int seconds1 = this.converttimeseconds(time1);
	int seconds2 = this.converttimeseconds(time2);
	int i = seconds1 - seconds2;
	if (i>=0) {
	return this.secondstotime(i);
	}else return "00:00:00";
	}
	
	}
	
}


